package com.eversec.view.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.eversec.util.ftp.EncryptConstant;
import com.eversec.util.ftp.FtpDataOper;
import com.eversec.util.ftp.FtpXmlUtil;

/**
 * 文本解密TabPanel页面
 * 
 * @author Administrator
 * 
 */
public class TextEncryptionPanel extends JPanel {

	private static final long serialVersionUID = 2616998568109305698L;

	private JTextArea beforeDecryptArea;
	private JButton encryptButton;
	private JButton decryptButton;
	private JButton clearAllButton;
	private JTextArea afterDecryptArea;
	
	public TextEncryptionPanel() {
		initComponents();
		initListener();
	}

	/**
	 * 初始化组件
	 */
	private void initComponents() {
		setLayout(new GridBagLayout());
		
		beforeDecryptArea = new JTextArea();
		JScrollPane beforeDecryptPane = new JScrollPane(beforeDecryptArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		Box mainBox = Box.createHorizontalBox();
		
		encryptButton = new JButton("加密");
		encryptButton.setEnabled(false);
		decryptButton = new JButton("解密");
		clearAllButton = new JButton("清空");
		
		mainBox.add(decryptButton);
		mainBox.add(Box.createHorizontalStrut(10));
//		mainBox.add(encryptButton);
//		mainBox.add(Box.createHorizontalStrut(10));
		mainBox.add(clearAllButton);
		
		
		afterDecryptArea = new JTextArea();
//		afterDecryptArea.setEnabled(false);
		JScrollPane afterDecryptPane = new JScrollPane(afterDecryptArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		add(beforeDecryptPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(10, 10, 0, 10), 0, 0));
		add(mainBox, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.NONE,
				new Insets(10, 10, 10, 10), 0, 0));
		add(afterDecryptPane, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(0, 10, 10, 10), 0, 0));
	}

	/**
	 * 初始化监听事件
	 */
	private void initListener() {
//		encryptButton.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				
//			}
//		});
		decryptButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String content = beforeDecryptArea.getText().trim();
				if (!"".equals(content)) {
					
					try {
						String xmlData = new FtpDataOper().ftpFileOper(content, EncryptConstant.aesKey, EncryptConstant.aesOffsets,
								EncryptConstant.hashcode, EncryptConstant.messageKey);
						String formatXML = FtpXmlUtil.formatXML(xmlData);
						afterDecryptArea.setText(formatXML);
					} catch (Exception e1) {
						afterDecryptArea.setText(e1.getMessage());
					}
				}
			}
		});
		clearAllButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				beforeDecryptArea.setText("");
				afterDecryptArea.setText("");
			}
		});
	}

}









